<?php

namespace app\apiadmin\service\behavior;

use app\apiadmin\model\behavior\ControlMongo;
use app\apiadmin\service\liveManager\LiveManagerService;
use app\apiadmin\service\system\MemberService;

class MessageService extends ControlMongo
{

    /**
     * 邀请记录
     *
     * */
    public static function invitationList($param)
    {
        $param['query'] = array_merge($param['query'] ?? [], ['del'=>0, 'status'=>1]);
        $total = parent::total($param['query'] ?? []);
        $list = parent::list($param['query'] ?? [], $param['options'] ?? []);
        return [
            'total' => $total,
            'list' => $list,
        ];
    }

    /**
     * 签到记录
     *
     * */
    public static function singList($param)
    {
        $param['query'] = array_merge($param['query'] ?? [], ['del'=>0, 'status'=>2]);
        $total = parent::total($param['query'] ?? []);
        $list = parent::list($param['query'] ?? [], $param['options'] ?? []);
        return [
            'total' => $total,
            'list' => $list,
        ];
    }

    /**
     * 删除记录
     *
     * */
    public static function delId(string $id = null): bool
    {
        if (empty($id)) {
            return false;
        }
        return parent::del($id);
    }

    /**
     * 更新或插入记录
     *
     * */
    public static function addOrEdit($param)
    {
        if (empty($param)) {
            return false;
        }
        $liveId = LiveManagerService::getLiveIdByGroupId($param['GroupId']);
        $live = LiveManagerService::getInfo($liveId);
        $msg_time = (int)($param['MsgTime'] - $live['service_start_time']);
        if ($param['Operator_Account'] == 'Administrator') {
            if ($param['From_Account'] == 'administrator') {
                // 中控信息
                $userType = '场控';
            } elseif (substr($param['From_Account'], 0, 1) === 'R') {
                // 数字人信息
                $userType = '数字人';
            } elseif (substr($param['From_Account'], 0, 1) === 'A') {
                //  中控直播端信息
                $userType = '助教';
            } elseif (substr($param['From_Account'], 0, 1) === 'P') {
                //  评论组信息
                $userType = '评论组';
            } else {
                // 铁粉信息
                $userType = '铁粉';
            }
        } else {
            if (substr($param['Operator_Account'], 0, 1) === 'R') {
                // 数字人信息
                $userType = '数字人';
            } elseif (substr($param['Operator_Account'], 0, 1) === 'A') {
                //  中控直播端信息
                $userType = '助教';
            } elseif (substr($param['Operator_Account'], 0, 1) === 'P') {
                //  评论组信息
                $userType = '评论组';
            } else {
                // 铁粉信息
                $userType = '铁粉';
            }
        }
        $data['query'] = [
            "live_id" => $liveId,
            "GroupId" => $param['GroupId'],  // 群ID
            "From_Account" => $param['From_Account'],  // 消息发送者
            "Operator_Account" => $param['Operator_Account'],  // 谁发起请求
            //"MsgSeq" => $param['MsgSeq'],  // 消息序列号，一条消息的唯一标示 群聊消息使用 MsgSeq 进行排序，MsgSeq 值越大消息越靠后，如果是在线消息，该 MsgSeq 则为随机值
            //"MsgTime" => $param['MsgTime'],  // 消息发送的时间戳，对应后台 Server 时间
            //"MsgBody" => $param['MsgBody'],  // 消息
            //"EventTime" => $param["EventTime"],  // 事件触发的毫秒级别时间戳
            "random_id" => $param['random_id'],  // 发消息请求中的32位随机数
            //"is_illegal" => $param['is_illegal'],
            //"is_silence" => $param['is_silence'],
            //"is_hide" => $param['is_hide'],
        ];
        $data['set'] = [
            '$set' => [
                "user_type" => $userType,
                "MsgSeq" => $param['MsgSeq'],
                "MsgTime" => $param['MsgTime'],
                "MsgBody" => $param['MsgBody'],
                "EventTime" => $param["EventTime"],
                "SendTime" => $msg_time,
                "is_illegal" => $param['is_illegal'],
                "is_silence" => $param['is_silence'],
                "is_hide" => $param['is_hide'],
            ]
        ];
        $data['options'] = ['upsert' => true];
        return parent::addOrEdit($data);
    }
    
    
}
